package com.example.teamtribe.main.club.manage.feedback.apply;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.CallbackStore;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.common.model.UserModel;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.database.User;
import com.example.teamtribe.json.JsonRequest;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;
import com.google.gson.Gson;

import org.litepal.crud.DataSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppListModel {

    private static AppListModel instance;
    private AppListModel() {}
    public static AppListModel getInstance() {
        if (instance == null) {
            synchronized (AppListModel.class) {
                if (instance == null) {
                    instance = new AppListModel();
                }
            }
        }
        return instance;
    }

    public List<TeamRequest> getLocal(int teamId) {
        return DataSupport.where("teamId = ?", teamId + "").find(TeamRequest.class);
    }

    public void getRequests(final String token, int id, Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        HttpUtil.post(URLStore.TEAM_REQUEST_GET, params, new CallbackStore.CallbackImpl<Back>(back) {
            @Override
            public void onResponse(String json) {
                JsonRequest jsonRequest = new Gson().fromJson(json, JsonRequest.class);
                TeamRequest teamRequest = jsonRequest.getRequest();
                Bundle bundle = new Bundle();
                bundle.putParcelable("request", teamRequest);
                back.success(bundle);
                UserModel.getInstance().getUser(token, teamRequest.getUserId(), new FileModel.FileBack() {
                    @Override
                    public void loadSuccess(String fileName) {

                    }

                    @Override
                    public void success(@Nullable Bundle bundle) {
                        User user = bundle.getParcelable("user");
                        user.saveOrUpdate();
                    }

                    @Override
                    public void failure(String meg) {
                        back.failure(meg);
                    }
                });
            }
        });
    }

    /**
     *
     * @param result 1同意，2拒绝
     */
    public void response(String token, int requestId, int result, String reason, Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", token);
        params.put("requestId", requestId + "");
        params.put("result", result + "");
        params.put("reason", reason);
        HttpUtil.post(URLStore.TEAM_RESPONSE_SEND, params, CallbackStore.getCallback(back));
    }
}
